<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../login.php");
    exit();
}

require '../config.php';

// 获取所有评论
$comments_per_page = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $comments_per_page;

$stmt = $conn->prepare("SELECT c.id, c.content, c.created_at, u.username, p.title 
    FROM comments c 
    JOIN users u ON c.user_id = u.id 
    JOIN posts p ON c.post_id = p.id 
    ORDER BY c.created_at DESC 
    LIMIT ?, ?");
$stmt->bind_param("ii", $offset, $comments_per_page);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($comment_id, $comment_content, $comment_created_at, $comment_username, $post_title);

// 获取总评论数
$total_comments_stmt = $conn->prepare("SELECT COUNT(*) FROM comments");
$total_comments_stmt->execute();
$total_comments_stmt->bind_result($total_comments);
$total_comments_stmt->fetch();
$total_pages = ceil($total_comments / $comments_per_page);

$total_comments_stmt->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理评论 - 论坛</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <style>
        .background-image {
            background-image: url('https://source.unsplash.com/1600x900/?nature,water'); /* 替换为合适的背景图片链接 */
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
        }
        .overlay {
            background: rgba(255, 255, 255, 0.9);
            backdrop-filter: blur(10px);
            border-radius: 1rem;
            padding: 2rem;
            max-width: 100%;
        }
        .btn-custom {
            transition: background-color 0.3s, color 0.3s;
        }
        .btn-custom:hover {
            background-color: #2563eb;
            color: white;
        }
        .pagination a {
            transition: background-color 0.3s, color 0.3s;
        }
        .pagination a:hover {
            background-color: #2563eb;
            color: white;
        }
        .sidebar {
            background: rgba(0, 0, 0, 0.6);
            color: white;
            padding: 1rem;
            border-radius: 1rem;
        }
        .main-content {
            background: rgba(255, 255, 255, 0.8);
            border-radius: 1rem;
            padding: 1rem;
        }
    </style>
</head>
<body class="background-image min-h-screen flex items-center justify-center p-4">
    <div class="overlay container mx-auto p-6 shadow-2xl flex w-full">
        <aside class="sidebar w-1/4 mr-8">
            <h2 class="text-2xl font-bold mb-4">管理评论</h2>
            <nav>
                <ul class="space-y-4">
                    <li><a href="index.php" class="text-lg font-medium btn-custom px-4 py-2 block rounded bg-gray-800 text-white">主页</a></li>
                    <li><a href="manage_users.php" class="text-lg font-medium btn-custom px-4 py-2 block rounded bg-gray-800 text-white">管理用户</a></li>
                    <li><a href="manage_comments.php" class="text-lg font-medium btn-custom px-4 py-2 block rounded bg-gray-800 text-white">管理评论</a></li>
                    <li><a href="../logout.php" class="text-lg font-medium btn-custom px-4 py-2 block rounded bg-red-600 text-white">退出登录</a></li>
                </ul>
            </nav>
        </aside>
        <main class="main-content flex-1">
            <section>
                <h3 class="text-2xl font-semibold mb-6 text-gray-700">所有评论</h3>
                <ul class="space-y-8">
                    <?php
                    if ($stmt->num_rows > 0) {
                        while ($stmt->fetch()) {
                            echo "<li class='bg-white p-6 rounded-lg shadow-md'>
                                <div class='comment-content mb-4'>" . htmlspecialchars($comment_content) . "</div>
                                <div class='comment-meta text-sm text-gray-500 mb-4'>发布者: " . htmlspecialchars($comment_username) . " | 帖子: " . htmlspecialchars($post_title) . " | 发布时间: $comment_created_at</div>
                                <div class='comment-actions'>
                                    <a href='delete_comment.php?id=$comment_id' class='btn btn-delete text-red-600 hover:text-red-800 px-4 py-2 rounded' onclick='return confirm(\"确定要删除这条评论吗？\");'>删除</a>
                                </div>
                            </li>";
                        }
                    } else {
                        echo "<li class='text-gray-500'>暂无评论</li>";
                    }
                    $stmt->close();
                    $conn->close();
                    ?>
                </ul>
                <div class="pagination mt-8 flex justify-center space-x-3">
                    <?php
                    if ($total_pages > 1) {
                        for ($i = 1; $i <= $total_pages; $i++) {
                            if ($i == $page) {
                                echo "<span class='current-page bg-blue-600 text-white px-4 py-2 rounded'>$i</span>";
                            } else {
                                echo "<a href='manage_comments.php?page=$i' class='bg-white text-blue-600 border border-blue-600 px-4 py-2 rounded hover:bg-blue-600 hover:text-white'>$i</a>";
                            }
                        }
                    }
                    ?>
                </div>
            </section>
        </main>
    </div>
</body>
</html>